//
//  ForgetPassWordViewController.m
//  EasyToTesco2.0
//
//  Created by 袖手旁觀 on 16/7/26.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "ForgetPassWordViewController.h"
#import "GetPhoneNumber.h"


#define Margin 8

@interface ForgetPassWordViewController ()<UITextFieldDelegate>
{
    UITextField *phoneNumberTextField;
    UITextField *againNewPasswordTextField;
    UITextField *newPasswordTextField;
    UITextField *yzmTextField;
    
    UILabel *_phoneLabel;
    NSString *_phoneString;
    NSString *_phoneNumberString;
    NSString *_username;
}

@end

@implementation ForgetPassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setNavUI];
    
    [self setUpUI];
}

- (void)setNavUI {
    
    self.navigationController.navigationBar.backgroundColor = DEFAULT_COLOR;
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"jiantou"] highImage:[UIImage imageNamed:@"jiantou"] target:self action:@selector(backToPre) forControlEvents:UIControlEventTouchUpInside title:nil];
    self.navigationItem.leftBarButtonItem.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    
    
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    customLab.textAlignment = NSTextAlignmentCenter;
    [customLab setText:@"忘记密码"];
    customLab.font = [UIFont boldSystemFontOfSize:18];
    self.navigationItem.titleView = customLab;
    
}

- (void)backToPre {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setUpUI {
    UILabel *inputPhone = [[UILabel alloc] init];
    inputPhone.text = @"账号:";
    inputPhone.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:inputPhone];
    inputPhone.font = FONT(15);
    
    [inputPhone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).with.offset(Margin * 2);
        make.top.equalTo(self.view.mas_top).with.offset(HEIGHT /7);
        make.width.mas_equalTo(WIDTH * 0.3);
        make.height.mas_equalTo(25);
    }];
    
    phoneNumberTextField = [[UITextField alloc] init];
    phoneNumberTextField.borderStyle = UITextBorderStyleRoundedRect;
    phoneNumberTextField.keyboardType = UIKeyboardTypeNumberPad;
    phoneNumberTextField.delegate = self;
    [phoneNumberTextField addTarget:self action:@selector(didClickPhoneNumberTextField:)forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:phoneNumberTextField];
    
    [phoneNumberTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(inputPhone.mas_right).with.offset(Margin * 2);
        make.top.equalTo(inputPhone.mas_top);
        make.width.mas_equalTo(WIDTH * 0.5);
        make.height.mas_equalTo(inputPhone.mas_height);
    }];
    
    UILabel *phoneLabel = [[UILabel alloc] init];
    phoneLabel.text = @"绑定手机号:";
    phoneLabel.textAlignment = NSTextAlignmentLeft;
    phoneLabel.font = FONT(15);
    [self.view addSubview:phoneLabel];
    
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(inputPhone.mas_bottom).with.offset(Margin * 3);
        make.left.equalTo(inputPhone.mas_left);
        make.width.mas_equalTo(inputPhone.mas_width);
        make.height.mas_equalTo(inputPhone.mas_height);
    }];
    
    _phoneLabel = [[UILabel alloc] init];
    _phoneLabel.text = @"************";
    _phoneLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:_phoneLabel];
    _phoneLabel.font = FONT(15);
    
    [_phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneNumberTextField.mas_left);
        make.top.equalTo(phoneLabel.mas_top);
        make.width.mas_equalTo(WIDTH * 0.5);
        make.height.mas_equalTo(_phoneLabel.mas_height);
    }];
    
    UILabel *yzmLabel = [[UILabel alloc] init];
    yzmLabel.text = @"请输入验证码:";
    yzmLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:yzmLabel];
    yzmLabel.font = FONT(15);
    [yzmLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneLabel.mas_bottom).with.offset(Margin * 3);
        make.left.equalTo(inputPhone.mas_left);
        make.width.mas_equalTo(inputPhone.mas_width);
        make.height.mas_equalTo(inputPhone.mas_height);
    }];
    
    yzmTextField = [[UITextField alloc] init];
    yzmTextField.borderStyle = UITextBorderStyleRoundedRect;
    yzmTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:yzmTextField];
    
    [yzmTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneNumberTextField.mas_left);
        make.top.equalTo(yzmLabel.mas_top);
        make.width.mas_equalTo(WIDTH * 0.25);
        make.height.mas_equalTo(yzmLabel.mas_height);
    }];
    
    
    UIButton *yzmButton = [[UIButton alloc] init];
    yzmButton.layer.cornerRadius = 4;
    yzmButton.layer.masksToBounds = YES;
    [yzmButton setBackgroundColor:DEFAULT_COLOR];
    [yzmButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [yzmButton addTarget:self action:@selector(didClickYZMButton:) forControlEvents:UIControlEventTouchUpInside];
    [yzmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    yzmButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:yzmButton];
    
    [yzmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(phoneNumberTextField.mas_right);
        make.centerY.equalTo(yzmTextField.mas_centerY);
        make.width.mas_equalTo(WIDTH * 0.25 - Margin);
        make.height.mas_equalTo(30);
    }];
    
    UILabel *newPasswordLabel = [[UILabel alloc] init];
    newPasswordLabel.text = @"请输入新密码:";
    [self.view addSubview:newPasswordLabel];
    newPasswordLabel.font = FONT(15);
    
    [newPasswordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(yzmLabel.mas_bottom).with.offset(Margin * 3);
        make.left.equalTo(yzmLabel.mas_left);
        make.width.mas_equalTo(yzmLabel.mas_width);
        make.height.mas_equalTo(yzmLabel.mas_height);
    }];
    
    newPasswordTextField = [[UITextField alloc] init];
    newPasswordTextField.borderStyle = UITextBorderStyleRoundedRect;
    newPasswordTextField.secureTextEntry = YES;
    [self.view addSubview:newPasswordTextField];
    
    [newPasswordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneNumberTextField.mas_left);
        make.top.equalTo(newPasswordLabel.mas_top);
        make.width.mas_equalTo(phoneNumberTextField.mas_width);
        make.height.mas_equalTo(yzmLabel.mas_height);
    }];
    
    UILabel *againPasswordLabel = [[UILabel alloc] init];
    againPasswordLabel.text = @"重新输入密码:";
    [self.view addSubview:againPasswordLabel];
    againPasswordLabel.font = FONT(15);
    [againPasswordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(newPasswordLabel.mas_left);
        make.top.equalTo(newPasswordLabel.mas_bottom).with.offset(Margin * 3);
        make.width.mas_equalTo(newPasswordLabel.mas_width);
        make.height.mas_equalTo(newPasswordLabel.mas_height);
    }];
    
    againNewPasswordTextField = [[UITextField alloc] init];
    againNewPasswordTextField.borderStyle = UITextBorderStyleRoundedRect;
    againNewPasswordTextField.secureTextEntry = YES;
    [self.view addSubview:againNewPasswordTextField];
    
    [againNewPasswordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneNumberTextField.mas_left);
        make.top.equalTo(againPasswordLabel.mas_top);
        make.width.mas_equalTo(phoneNumberTextField.mas_width);
        make.height.mas_equalTo(yzmLabel.mas_height);
    }];
    
    UIButton *confirmButton = [[UIButton alloc] init];
    confirmButton.layer.cornerRadius = 5;
    confirmButton.layer.masksToBounds = YES;
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmButton setBackgroundColor: DEFAULT_COLOR];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(didClickConfirmButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmButton];
    
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(againNewPasswordTextField.mas_bottom).with.offset(HEIGHT * 0.1 - Margin);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(WIDTH * 0.5);
        make.height.mas_equalTo(30);
    }];
}

- (void)didClickPhoneNumberTextField:(UITextField *)textField {
    
    if (textField == phoneNumberTextField) {
        if (textField.text.length >=11) {
            textField.text = [textField.text substringToIndex:11];
        }
        if (textField.text.length >10) {
            _username = textField.text;
            [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:@{@"username":textField.text} AndMenthodName:@"api/User/info" completeBlock:^(id object) {
                if (JudgeBackDataCode) {
                    _phoneString = [NSString stringWithFormat:@"%@",[object[@"data"] objectForKey:@"phone"]];
                    if (_phoneString.length) {
                        _phoneLabel.text = [self stringForBankNumber:_phoneString];
                    } else {
                        _phoneLabel.text = @"该账号尚未绑定手机";
                    }
                } else {
                    [self showHint:[NSString stringWithFormat:@"%@",[object[@"message"] objectForKey:@"msg"]]];
                }
            }];
            
        } else {
            _username = @"";
            _phoneLabel.text = @"************";
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString *)stringForBankNumber:(NSString *)string{
    _phoneNumberString = string;
    NSString *left,*right,*center = [NSString string];
    left = [string substringWithRange:NSMakeRange(0, 3)];
    NSInteger length = string.length;
    center = @"****";
    right = [string substringWithRange:NSMakeRange(length - 4, 4)];
    return [NSString stringWithFormat:@"%@%@%@",left,center,right];
}

/**
 *  验证码
 */
- (void)didClickYZMButton:(UIButton *)button {
    [self clearTextFieldResign];
    
    if (!_username.length) {
        [self showHint:@"请输入正确的账号"];
        return;
    }
    
    if (!_phoneNumberString.length) {
        [self showHint:@"改账号尚未绑定手机"];
        return;
    }
    
    [[GetPhoneNumber SharedGetPhoneNumber] getFotPhoneVerifyNumber:button andPhoneNumber:_phoneNumberString andView:self.view andGetVerifyType:@"1" andUserName:_username];
    
}

/**
 *  确定按钮
 */
- (void)didClickConfirmButton {
    
    NSString *phoneNumber = phoneNumberTextField.text;
    NSString *yzmString = yzmTextField.text;
    NSString *newPassWord = newPasswordTextField.text;
    NSString *againNewPassWord = againNewPasswordTextField.text;
    
    if ((phoneNumber.length == 0) || (yzmString.length == 0) || (newPassWord.length == 0) || (againNewPassWord.length == 0)) {
        
        MBProgressHUD *hud = [MBProgressHUD showMessage:@"请确认信息输入无误" toView:self.view dimBackground:NO AndCompleteBlock:nil];
        
        [self.view addSubview:hud];
    } else if (![newPassWord isEqualToString:againNewPassWord]) {
        MBProgressHUD *hud = [MBProgressHUD showMessage:@"两次密码输入有误,请重试" toView:self.view dimBackground:NO AndCompleteBlock:nil];
        
        [self.view addSubview:hud];
    } else {
        
        NSDictionary *params = @{
                                 @"username":phoneNumber,
                                 @"newpassword":againNewPassWord,
                                 @"verify":yzmString
                                 };
        
        [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/Pub/forgetpassword" completeBlock:^(id object) {
            
            [JudgeBackDataCodeMessage JudgeBackDataCodeMessage:@"密码重置成功" andObjece:object andAddHUDView:self.view andPopViewController:^(id object) {
                [self.navigationController popViewControllerAnimated:YES];
            }];
            
        }];
        
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self clearTextFieldResign];
}

- (void)clearTextFieldResign {
    [phoneNumberTextField resignFirstResponder];
    [againNewPasswordTextField resignFirstResponder];
    [newPasswordTextField resignFirstResponder];
    [yzmTextField resignFirstResponder];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
